﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ChinaDynastyTours.Controls;
using ChinaDynastyTours.Helpers;

namespace ChinaDynastyTours.Views
{
    public partial class HotelOrderServiceForm : DevExpress.XtraEditors.XtraForm
    {
        //输入参数
        public int _TourGroupScheduleID { get; set; }//行程计划ID
        public string _ProviderID { get; set; }//如存在预订的宾馆供应商，则传进来

        private DataTable _dtHotelOrderData;//钱款信息，
        
        //输出参数
        public string ReturnProviderId { get; set; }
        public string ReturnProviderName { get; set; }
        public string ReturnContractPath { get; set; }
        public string ReturnServiceName { get; set; }

        public HotelOrderServiceForm(string desCityName)
        {
            InitializeComponent();
        
            tbCityName.EditValue = desCityName;
        }

        private void LoadData()
        {
            hotelGridControl.DataSource = BaseInfoCtrl.GetProviderData(new ProviderTypeEnum[] { ProviderTypeEnum.宾馆 }, CN2PYHelper.CapitalCovert(tbCityName.Text), CN2PYHelper.CapitalCovert(tbProviderName.Text));
        }

        private void HotelOrderServiceForm_Load(object sender, EventArgs e)
        {
            //初始化旅行社数据
            //hotelGridControl.DataSource = BaseInfoCtrl.GetProviderData(new ProviderTypeEnum[] { ProviderTypeEnum.宾馆 });
            LoadData();

            _dtHotelOrderData = HotelOrderCtrl.GetHotelOrderData(_TourGroupScheduleID);

            //设定焦点行
            if (_ProviderID == "") return;
            for (int i = 0; i < hotelGridView.RowCount; i++)
            {
                if (_ProviderID == hotelGridView.GetRowCellValue(i, "ProviderID").ToString())
                    hotelGridView.FocusedRowHandle = i;
            }

        }
        private void hotelGridView_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (e.FocusedRowHandle < 0) return;

            string providerId = hotelGridView.GetDataRow(e.FocusedRowHandle)["ProviderID"].ToString();
            if (string.IsNullOrEmpty(providerId)) return;
            hotelServiceGridControl.DataSource = ServiceInfoCtrl.GetHotelServiceData(providerId);
        }

        private void hotelServiceGridView_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            //双击服务表格，将单价写入文本框中
            if (e.Clicks == 2)
                btnOk.PerformClick();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            ReturnProviderId = hotelGridView.GetDataRow(hotelGridView.FocusedRowHandle)["ProviderID"].ToString();
            ReturnProviderName = hotelGridView.GetDataRow(hotelGridView.FocusedRowHandle)["ProviderName"].ToString();
            ReturnContractPath=hotelGridView.GetDataRow(hotelGridView.FocusedRowHandle)["Contact"].ToString();

            if (hotelServiceGridView.FocusedRowHandle < 0 || chkSelectedService.Checked==true)
                ReturnServiceName = "";
            else
            {
                int[] rowIds = hotelServiceGridView.GetSelectedRows();
                for (int i = 0; i < rowIds.Length; i++)
                {
                    ReturnServiceName += hotelServiceGridView.GetDataRow(rowIds[i])["FeaturesRoom"].ToString();
                    if (i != rowIds.Length - 1)
                        ReturnServiceName += "|";
                }
            }
        }
        private void TextBox_TextChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        private void chkSelectedService_CheckedChanged(object sender, EventArgs e)
        {
            hotelServiceGridControl.Enabled = !chkSelectedService.Checked;
        }
    }
}
